/**
 * Search list handling
 */
var search_videos_list = {
	
	list : {},
	
	template : function() {
		debug.print("search_videos_list.template");
		var ret = "\
<div class='result'>\
	<div class='result-cover'>\
		<img src='' width='57' height='56' alt='' />\
	</div>\
	<div class='result-artist'><span class='artist-part'></span><span class='album-part'></span></div>\
	<div class='result-title'></div>\
	<div class='result-meta'></div>\
	<div class='result-controls'>\
		<div class='addto-np'><a href='javascript:void(0);'>add to now playing</a></div>\
		<div class='cue-left'><a href='javascript:void(0);'>cue left</a></div>\
		<div class='cue-right'><a href='javascript:void(0);');'>cue right</a></div>\
	</div>\
</div>";
		return ret;		
	},
	
	add : function(obj, start) {
		debug.print("search_videos_list.add [obj: " + obj + "]");
		var sr = $("#search-results");
		sr.append("<div id='search-resultset-"+start+"'></div>");
		var set = $("#search-resultset-"+start).hide();
		for (var i in obj) {
			this.list[i] = obj[i];
			this.renderItem(this.list[i], set)
		}
		set.slideDown(2500);
	},
	
	renderItem : function(item, set) {
		debug.print("search_videos_list.renderItem [item: " + item + ", set: " + set + "]");
		set.append(this.template());
		var si = set.find(".result:last");
		
		si.data("video", item);
				
		si.find(".result-cover img").attr("src", item.thumbnail);
		si.find(".artist-part").text(item.artist);
		si.find(".result-title").text(item.title);
		if (item.album) {
			si.find(".album-part").text(item.album);
		}
		if ($("#debug").length) {
			// add youtube id link
			si.find(".result-meta").html(item.duration + " &nbsp; <a target='_blank' href='http://www.youtube.com/watch?v=" + item.id + "'>youtube.com</a>: <a href=\"javascript:prompt('Copy the video ID to clipboard','" + item.id + "');\">" + item.id + "</a>");
		} else {
			si.find(".result-meta").html(item.duration + " &nbsp; <a target='_blank' href='http://www.youtube.com/watch?v=" + item.id + "'>youtube.com</a>");
		}
		si.dblclick(function() { controller.addToNp(item); });
		si.find(".addto-np").click(function() { controller.addToNp(item); });
		si.find(".cue-left").click(function() { controller.cue(0, item); });
		si.find(".cue-right").click(function() { controller.cue(1, item); });
	},
	
	clear : function(callback) {
		var sr = $("#search-results");
		$(sr).fadeOut(function() {
			this.list = {};
			sr.empty().show();
			callback();
		});
	},
	
	count : function() {
		debug.print("search_videos_list.count");
		return $("#search-results .result").length;
	},
	
	getFirst : function() {
		debug.print("search_videos_list.getFirst");
		return $("#search-results .result:first");
	}
	
}